<template>
  <div class="app-container">
    <!-- 条件查询渲染 -->
    <div>
      <el-input v-model="pageDto.where.propertyName" placeholder="请输入模型名称"
        style="width: 250px;margin-left: 20px;"></el-input>
      <el-select v-model="pageDto.where.deviceTypeId" placeholder="请选择设备类型" style="width: 250px;margin-left: 20px;">
        <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="pageDto.where.modelType" placeholder="请选择物模型类型" style="width: 250px;margin-left: 20px;">
        <el-option key="1" label="属性" value="1"></el-option>
        <el-option key="2" label="事件" value="2"></el-option>
        <el-option key="3" label="服务" value="3"></el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 20px;" @click="pageDto.now = 1;search()">搜索</el-button>
      <el-button type="danger" icon="el-icon-refresh" @click="reset">清空</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-right: 20px;float: right;"
        @click="dialogFormVisible = true; modelTemplate = {}; add()">添加</el-button>
    </div>

    <!-- 页面表格渲染 -->
    <div style="margin-top: 20px;">
      <el-table :data="modelList" style="width: 100%;margin-bottom: 20px;"
        :header-cell-style="{ background: '#F7F7F8', color: '#18191B', 'text-align': 'center' }">
        <el-table-column prop="propertyName" label="物模型名称" align="center"> </el-table-column>
        <el-table-column prop="deviceTypeName" label="设备类型" align="center"> </el-table-column>
        <el-table-column label="物模型类型" align="center">
          <!-- 自定义渲染 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.modelType == 1" key="属性" type="success">
              {{ '属性' }}
            </el-tag>
            <el-tag v-if="scope.row.modelType == 2" key="事件">
              {{ '事件' }}
            </el-tag>
            <el-tag v-if="scope.row.modelType == 3" key="服务" type="warning">
              {{ '服务' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link @click="edit(scope.row)" type="primary" :underline="false">编辑</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageDto.now"
        :page-sizes="[5, 10, 50, 100]" :page-size="pageDto.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total" style="text-align: center;">
      </el-pagination>
    </div>

    <!-- 添加表格渲染 -->
    <el-dialog :title="modelTemplate.id?'编辑物模型':'添加物模型'" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="modelTemplate" :rules="rules" ref="modelTemplate" label-width="150px" class="demo-ruleForm"
        size="mini">
        <el-form-item label="物模型名称" prop="propertyName">
          <el-input v-model="modelTemplate.propertyName" placeholder="请输入设备类型名称" style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item label="设备所属行业" prop="deviceTypeId">
          <el-select v-model="modelTemplate.deviceTypeId" placeholder="请选择设备类型" style="width: 250px;">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="modelType">
          <el-select v-model="modelTemplate.modelType" placeholder="请选择物模型类型" style="width: 250px;">
            <el-option key="1" label="属性" value="1"></el-option>
            <el-option key="2" label="事件" value="2"></el-option>
            <el-option key="3" label="服务" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" placeholder="请输入内容" v-model="modelTemplate.description" style="width: 250px;">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('modelTemplate')">重置</el-button>
        <el-button @click="dialogFormVisible = false; resetForm('modelTemplate')">取 消</el-button>
        <el-button type="primary" @click="submitForm('modelTemplate')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { page, type, add } from "@/api/modelTemplate"
export default {
  data() {
    return {
      rules: {
        propertyName: [
          { required: true, message: '请输入物模型名称', trigger: 'blur' },
        ],
        deviceTypeId: [
          { required: true, message: '请选择设备类型', trigger: 'blur' },
        ],
        modelType: [
          { required: true, message: '请选择物模型类型', trigger: 'blur' },
        ],
      },
      dialogFormVisible: false,
      //表格数据
      modelList: [],
      //设备类型条件查询数据
      typeList: [],
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      pages: 0,
      total: 0,
      modelTemplate: {},

    }
  },
  methods: {
    //下拉框数据回显导致数据卡格
    getIndustryId() {
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },

    //下拉框数据回显导致数据卡格
    getDeviceType() {
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },

    //提交更新
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.modelTemplate).then((res) => {
            if (res.code == 0) {
              this.dialogFormVisible = false;
              this.$message({
                message: res.msg,
                type: 'success'
              });
              this.modelTemplate = {}
              this.search()
            } else {
              this.$message.error(res.msg);
            }
          })
        } else {
          this.$message.error('关键信息未填写！');
          return false;
        }
      });
    },

    //重置更新
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //查询页面所有数据
    search() {
      page(this.pageDto).then((res) => {
        this.modelList = res.list;
        this.pages = res.pages;
        this.total = res.total;
      });
      type().then((res) => {
        this.typeList = res
      });
    },

    //分页页面显示数据多少
    handleSizeChange(size) {
      this.pageDto.size = size;
      this.search();
    },

    //分页页面当前页
    handleCurrentChange(now) {
      this.pageDto.now = now;
      this.search();
    },

    //重置条件查询
    reset() {
      this.pageDto.where = {}
    },

    //编辑
    edit(data) {
      this.modelTemplate = data
      this.modelTemplate.modelType = data.modelType + ""
      this.dialogFormVisible = true
    }
  },
  created() {
    this.search()
  }
}
</script>

<style></style>
